package cn.s1995.sloth.bizs.entity.base;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import static cn.hutool.core.date.DatePattern.NORM_DATETIME_PATTERN;

/**
 * BaseQueryEntity
 *
 * @author Lex
 * @description 基础查询实体
 * @date 2024/01/18 11:44
 */
@Getter
@Setter
public class BaseQueryEntity implements Serializable {
    //     -------------------------------通用查询参数
    // @Schema(description = "zz-通用ID")
    // @TableField(exist = false)
    // private Long id;

    @Schema(description = "zz-搜索关键字")
    @TableField(exist = false)
    private String search;

    @Schema(description = "zz-日期范围，eg: 2022-10-01 12:00:00,2022-10-30 12:00:00")
    @TableField(exist = false)
    private String[] rangeTime;

    // @Schema(description = "zz-偏移量，每次请求分页接口获取到数据累加数量（默认：0）")
    // @TableField(exist = false)
    // private Long offset = 0L;
    //
    // @Schema(description = "zz-每页显示条数（默认：10）")
    // @TableField(exist = false)
    // private Long pageSize = 10L;
    //
    // @Schema(description = "zz-排序字段，eg: sort|id|amount")
    // @TableField(exist = false)
    // private String orderByField = "";
    //
    // @Schema(description = "zz-排序方向（默认正序，注意大写）：正序-ASC 倒叙-DESC")
    // @TableField(exist = false)
    // private String orderByDirection = "ASC";

    /**
     * 判断是否是日期范围&时间是否有效
     *
     * @return
     */
    public boolean hasRangeTime() {
        String[] array = this.getRangeTime();
        boolean b = array != null && array.length == 2;
        boolean b2 = Boolean.FALSE;
        if (b) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(NORM_DATETIME_PATTERN);
            LocalDateTime start = LocalDateTimeUtil.parse(array[0], formatter);
            LocalDateTime end = LocalDateTimeUtil.parse(array[1], formatter);
            // 存在两个时间&&(开始时间要在结束时间之前||两个时间相等)
            b2 = (start.isBefore(end) || start.isEqual(end));
        }

        return b && b2;
    }

    /**
     * 判断是否是同一天
     *
     * @return
     */
    public boolean hasSameDay() {
        String[] array = this.getRangeTime();
        boolean b = array != null && array.length == 2;
        boolean b2 = Boolean.FALSE;
        boolean b3 = Boolean.FALSE;
        if (b) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(NORM_DATETIME_PATTERN);
            LocalDateTime start = LocalDateTimeUtil.parse(array[0], formatter);
            LocalDateTime end = LocalDateTimeUtil.parse(array[1], formatter);
            // 存在两个时间&&(开始时间要在结束时间之前||两个时间相等)
            b2 = (start.isBefore(end) || start.isEqual(end));

            // 是同一天
            b3 = LocalDateTimeUtil.isSameDay(start, end);
        }

        return b && b2 && b3;
    }


}
